﻿namespace EggOfPanKu.Main.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using EggOfPanKu.Library.Objects;
    using EggOfPanKu.Library.Infrastructure;
    using EggOfPanKu.Library;

    /// <summary>
    /// Interaction logic for CardControl.xaml
    /// </summary>
    public partial class CardControl : UserControl
    {
        #region Fields

        private Card _Card;
        private int _ControlID;

        #endregion Fields

        #region Constructors

        public CardControl()
        {
            InitializeComponent();
        }

        public CardControl(Card cardInput)
        {
            //Initialize UI controls
            InitializeComponent();
            //Set Card object and load card Image
            Card = cardInput;
            LoadImage(Card.ImagePath);
            //Initialize RightClick menu items
            InitializeMenuItems();
        }

        #endregion Constructors

        #region Delegates

        //Delegate used to bubble Menu Item Change events up to the page
        public delegate void BowCardHandler(object sender, RoutedEventArgs e);

        //Delegate used to bubble Menu Item Change events up to the page
        public delegate void DishonorCardHandler(object sender, RoutedEventArgs e);

        //Delegate used to bubble Menu Item Change events up to the page
        public delegate void ViewCardHandler(object sender, RoutedEventArgs e);

        #endregion Delegates

        #region Events

        //Event used to instantiate the delegate, and call it
        public event BowCardHandler BowCard;

        //Event used to instantiate the delegate, and call it
        public event DishonorCardHandler DishonorCard;

        //Event used to instantiate the delegate, and call it
        public event ViewCardHandler ViewCard;

        #endregion Events

        #region Properties

        public Card Card
        {
            get { return _Card; }
            set { _Card = value; }
        }

        public int ControlID
        {
            get { return _ControlID; }
            set { _ControlID = value; }
        }

        #endregion Properties

        #region Methods

        public void FlipCard()
        {
            if (!Card.IsFlipped)
            {
                switch (Card.DeckType)
                {
                    case CardDeck.Fate:
                        LoadImage(System.IO.Path.Combine(ConfigManager.AppSettingsMgr.CardImagePath, string.Format("{0}_back.jpg", CardDeck.Fate.ToString().ToLower())));
                        break;
                    case CardDeck.Dynasty:
                        LoadImage(System.IO.Path.Combine(ConfigManager.AppSettingsMgr.CardImagePath, string.Format("{0}_back.jpg", CardDeck.Dynasty.ToString().ToLower())));
                        break;
                }
            }
            else
            {
                LoadImage(Card.ImagePath);
            }

            Card.IsFlipped = !Card.IsFlipped;
        }

        public void InitializeMenuItems()
        {
            if (Card.IsBowed)
            {
                Menu_Bow.Header = "Straighten";
            }
            else
            {
                Menu_Bow.Header = "Bow";
            }

            if (Card.IsFlipped)
            {
                Menu_Flip.Header = "Flip face-up";
            }
            else
            {
                Menu_Flip.Header = "Flip face-down";
            }

            if (Card.IsDishonored)
            {
                Menu_Dishonor.Header = "Rehonor";
            }
            else
            {
                Menu_Dishonor.Header = "Dishonor";
            }

            if (Card.IsDynasty)
            {
                Menu_PutOnDeckBottom.Header = "Put on bottom of Dynasty Deck";
                Menu_PutOnDeckTop.Header = "Put on top of Dynasty Deck";
            }
            else if (Card.IsFate)
            {
                Menu_PutOnDeckBottom.Header = "Put on bottom of Dynasty Deck";
                Menu_PutOnDeckTop.Header = "Put on top of Dynasty Deck";
            }
            else
            {
                Menu_PutOnDeckBottom.Visibility = Visibility.Collapsed;
                Menu_PutOnDeckTop.Visibility = Visibility.Collapsed;
            }
        }

        public void LoadImage(string imagePath)
        {
            CardImage.Source = ImageManager.GenerateImageSource(imagePath);
        }

        public void PreviewCard()
        {
            ViewCard(this, null);
        }

        private void AddMarker_Click(object sender, RoutedEventArgs e)
        {
        }

        private void AddToken_Click(object sender, RoutedEventArgs e)
        {
        }

        private void Bow_Click(object sender, RoutedEventArgs e)
        {
            Card.IsBowed = !Card.IsBowed;
            BowCard(this, e);
        }

        private void ContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            InitializeMenuItems();
        }

        private void DestroyAndDiscard_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Discard_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Dishonor_Click(object sender, RoutedEventArgs e)
        {
            Card.IsDishonored = !Card.IsDishonored;
            DishonorCard(this, e);
        }

        private void Flip_Click(object sender, RoutedEventArgs e)
        {
            FlipCard();
        }

        private void PutOnDeckBottom_Click(object sender, RoutedEventArgs e)
        {
        }

        private void PutOnDeckTop_Click(object sender, RoutedEventArgs e)
        {
        }

        private void View_Click(object sender, RoutedEventArgs e)
        {
            ViewCard(this, e);
        }

        #endregion Methods

    }
}